package de.recipeminer.ui.commands.listing;

import de.recipeminer.mining.TermFrequencyImpl;
import de.recipeminer.mining.TermFrequencyImpl.TermFrequencyDictionary;
import de.recipeminer.tools.Config;
import de.recipeminer.tools.LimitablePrintStream;
import de.recipeminer.ui.commands.UserCommand;
import de.recipeminer.ui.parsing.OptionParser;
import joptsimple.OptionSet;
import org.apache.log4j.Logger;

import static java.util.Arrays.asList;

/**
 * Computes new Stopwords in desired amount.
 * Lists Stopwords in desired amount.
 *
 * @autor: Georg Mühlenberg
 */
public class ListStopwordsCommand extends UserCommand.ListingCommand {
    private static Logger logger = Logger.getLogger(ListStopwordsCommand.class);
    public static final String NAME = "list_stopwords";

    @Override
    public boolean run(OptionSet options, LimitablePrintStream out) {
        int newStopWordCandidatesToCompute = 20;
        if (options.has("newstopwords")) {
            newStopWordCandidatesToCompute = (Integer) options.valueOf("newstopwords");
        }
        String fileToWriteTo = Config.STOPWORD_FILE_LOCATION;
        if (options.has("export")) {
            fileToWriteTo = (String) options.valueOf("export");
        }

        TermFrequencyImpl timpl = TermFrequencyImpl.getInstance();
        
        timpl.writeStopWordFile(fileToWriteTo,
                timpl.termFrequencyDictionary()
                        .getStopWordCandidates(newStopWordCandidatesToCompute));
        out.print("\n" + newStopWordCandidatesToCompute + " new stopwords computed. \n"
                + "Stopword file written to " + fileToWriteTo);

        return true;


    }

    @Override
    public String commandName() {
        return NAME;
    }

    @Override
    public void extendOptions(OptionParser parser) {
//        parser.acceptsAll(asList("s", "stats"), "Also print frequency statistics.").withRequiredArg().ofType(boolean.class).defaultsTo(false);
        parser.accepts("export", "Path to file to write to")
                .withOptionalArg().withValuesConvertedBy(OptionParser.REGEXP_CONVERTER)
                .describedAs("file").defaultsTo(OptionParser.REGEXP_CONVERTER.convert(Config.STOPWORD_FILE_LOCATION));
        parser.accepts("newstopwords", "Compute new stopword candidates").withRequiredArg().ofType(Integer.class).defaultsTo((int) 20);
    }
    
}
